package com.mcxx.modules.system.service.read;

import com.mcxx.modules.system.dao.read.RuleObjectReadDao;
import com.mcxx.modules.system.dto.RuleItemDTO;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service
@TaTransactional(readOnly = true)
public class RuleObjectReadService {
    @Autowired
    private RuleObjectReadDao ruleObjectReadDao;


    public List<RuleItemDTO> getRuleItem(String businessType){

        List<RuleItemDTO> ruleItemList = ruleObjectReadDao.getRuleItem(businessType);

        Map<String, RuleItemDTO> ruleItemDTOMap = new HashMap<>();
        for(RuleItemDTO ruleItemDTO : ruleItemList){
            if(ruleItemDTOMap.keySet().contains(ruleItemDTO.getParentId())){
                ruleItemDTOMap.get(ruleItemDTO.getParentId()).getChildrenList().add(ruleItemDTO);
            }else{
                ruleItemDTOMap.put(ruleItemDTO.getId(), ruleItemDTO);
            }
        }

        return new ArrayList<>(ruleItemDTOMap.values());
    }
}
